<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Client extends CI_Controller {
	public function __construct()
    {
        parent::__construct();
		$this->load->helper('url');
		$this->load->library('session');
		$this->load->helper(array('form', 'url'));
		$this->lang->load('english', 'english');
		if($this->session->userdata('logged_in')){
			if($this->session->userdata('user_type')==0){
				redirect('admin');
			}
		}else{
			redirect('login');
		}
    }
    private function _isDemo(){
    	if($this->session->userdata('username') == "demowife" || $this->session->userdata('username') == "demohus"){
    		return true;
    	}
    	return false;
    }
	
	public function index()
	{
		$this->load->model('Usermodel');
		$this->load->model('UserDataModel');
		$adminInfor = $this->Usermodel->getAdminInfo();
		$data['adminInfor'] = $adminInfor;
		$userId = $this->session->userdata('userid');
		$user = $this->Usermodel->getUserById($userId);
		$data['user'] = $user;
		$userData = $this->UserDataModel->getUserData($userId, 1);
		
		if($this->input->post('nextstep')){
			if($this->input->post('step') == "0"){
				$this->load->view('index_step1_0',$data);	
			}else if($this->input->post('step') == "1_0"){
				$userData = $this->UserDataModel->getUserData($userId, 1);
				$data['userData'] = $userData;
				$this->load->view('index_step1_1',$data);
			}else if($this->input->post('step') == "1_1"){
				$trust = $this->input->post('trust-score');
				if($trust < 0){
					$trust = 0;
				}
				$respect = $this->input->post('respect-score');
				if($respect < 0){
					$respect = 0;
				}
				$care = $this->input->post('care-score');
				if($care < 0){
					$care = 0;
				}
				$commitment = $this->input->post('commitment-score');
				if($commitment < 0){
					$commitment = 0;
				}
				$financial_support = $this->input->post('financial-score');
				if($financial_support < 0){
					$financial_support = 0;
				}
				$domestric_support = $this->input->post('domestric-score');
				if($domestric_support < 0){
					$domestric_support = 0;
				}
				$intimacy = $this->input->post('intimacy-score');
				if($intimacy < 0){
					$intimacy = 0;
				}
				$physical_appearance = $this->input->post('physical-score');
				if($physical_appearance < 0){
					$physical_appearance = 0;
				}
				$fun_time_together = $this->input->post('fun-time-score');
				if($fun_time_together < 0){
					$fun_time_together = 0;
				}
				$best_friend_talk = $this->input->post('best-friend-score');
				if($best_friend_talk < 0){
					$best_friend_talk = 0;
				}
				$spiritual_intimacy = $this->input->post('spiritual-intimacy-score');
				if($spiritual_intimacy < 0){
					$spiritual_intimacy = 0;
				}
				$conflict_resolution = $this->input->post('conflict-resolution-score');
				if($conflict_resolution < 0){
					$conflict_resolution = 0;
				}
				
				date_default_timezone_set(date_default_timezone_get());
				
				$udate = time();
				$cdate = time();
				
				$isPresent = 1;
				$userData = $this->UserDataModel->getUserData($userId, 1);
				
				if(!$this->_isDemo()){
					if($userData){
						$userDataArray = array($trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $udate, $userData->id);
						$this->UserDataModel->updateUserData($userDataArray);
					}else{
						$userDataArray = array($userId, $trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $isPresent,$cdate, $udate );				
						$this->UserDataModel->inserUserData($userDataArray);
					}
				}
				$userData = $this->UserDataModel->getUserData($userId, 1);
				$data['userData'] = $userData;
				$this->load->view('index_step1_2',$data);
				
			}else if($this->input->post('step') == "1_2"){
				$this->load->view('index_step2_0',$data);
			}else if($this->input->post('step') == "2_0"){
				$userData = $this->UserDataModel->getUserData($userId, 0);
				$data['userData'] = $userData;
				$this->load->view('index_step2_1',$data);
			}else if($this->input->post('step') == "2_1"){
				$trust = $this->input->post('trust-score');
				if($trust < 0){
					$trust = 0;
				}
				$respect = $this->input->post('respect-score');
				if($respect < 0){
					$respect = 0;
				}
				$care = $this->input->post('care-score');
				if($care < 0){
					$care = 0;
				}
				$commitment = $this->input->post('commitment-score');
				if($commitment < 0){
					$commitment = 0;
				}
				$financial_support = $this->input->post('financial-score');
				if($financial_support < 0){
					$financial_support = 0;
				}
				$domestric_support = $this->input->post('domestric-score');
				if($domestric_support < 0){
					$domestric_support = 0;
				}
				$intimacy = $this->input->post('intimacy-score');
				if($intimacy < 0){
					$intimacy = 0;
				}
				$physical_appearance = $this->input->post('physical-score');
				if($physical_appearance < 0){
					$physical_appearance = 0;
				}
				$fun_time_together = $this->input->post('fun-time-score');
				if($fun_time_together < 0){
					$fun_time_together = 0;
				}
				$best_friend_talk = $this->input->post('best-friend-score');
				if($best_friend_talk < 0){
					$best_friend_talk = 0;
				}
				$spiritual_intimacy = $this->input->post('spiritual-intimacy-score');
				if($spiritual_intimacy < 0){
					$spiritual_intimacy = 0;
				}
				$conflict_resolution = $this->input->post('conflict-resolution-score');
				if($conflict_resolution < 0){
					$conflict_resolution = 0;
				}
				
				date_default_timezone_set(date_default_timezone_get());
				
				$udate = time();
				$cdate = time();
				
				$isPresent = 0;
				$userData = $this->UserDataModel->getUserData($userId, 0);
				
				if(!$this->_isDemo()){
					if($userData){
						$userDataArray = array($trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $udate, $userData->id);
						$this->UserDataModel->updateUserData($userDataArray);
					}else{
						$userDataArray = array($userId, $trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $isPresent,$cdate, $udate );				
						$this->UserDataModel->inserUserData($userDataArray);
					}
				}
				$userData = $this->UserDataModel->getUserData($userId, 0);
				
				$data['userData'] = $userData;
				$this->load->view('index_step2_2',$data);
			}else if($this->input->post('step') == "2_2"){
				$this->load->view('index_step3_0',$data);
			}else if($this->input->post('step') == "3_0"){
				$userData = $this->UserDataModel->getUserData($userId, 2);
				$data['userData'] = $userData;
				$this->load->view('index_step3_1',$data);
			}else if($this->input->post('step') == "3_1"){
				$trust = $this->input->post('trust-score');
				if($trust < 0){
					$trust = 0;
				}
				$respect = $this->input->post('respect-score');
				if($respect < 0){
					$respect = 0;
				}
				$care = $this->input->post('care-score');
				if($care < 0){
					$care = 0;
				}
				$commitment = $this->input->post('commitment-score');
				if($commitment < 0){
					$commitment = 0;
				}
				$financial_support = $this->input->post('financial-score');
				if($financial_support < 0){
					$financial_support = 0;
				}
				$domestric_support = $this->input->post('domestric-score');
				if($domestric_support < 0){
					$domestric_support = 0;
				}
				$intimacy = $this->input->post('intimacy-score');
				if($intimacy < 0){
					$intimacy = 0;
				}
				$physical_appearance = $this->input->post('physical-score');
				if($physical_appearance < 0){
					$physical_appearance = 0;
				}
				$fun_time_together = $this->input->post('fun-time-score');
				if($fun_time_together < 0){
					$fun_time_together = 0;
				}
				$best_friend_talk = $this->input->post('best-friend-score');
				if($best_friend_talk < 0){
					$best_friend_talk = 0;
				}
				$spiritual_intimacy = $this->input->post('spiritual-intimacy-score');
				if($spiritual_intimacy < 0){
					$spiritual_intimacy = 0;
				}
				$conflict_resolution = $this->input->post('conflict-resolution-score');
				if($conflict_resolution < 0){
					$conflict_resolution = 0;
				}
				
				date_default_timezone_set(date_default_timezone_get());
				
				$udate = time();
				$cdate = time();
				
				$isPresent = 2;
				$userData = $this->UserDataModel->getUserData($userId, 2);
				if(!$this->_isDemo()){
					if($userData){
						$userDataArray = array($trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $udate, $userData->id);
						$this->UserDataModel->updateUserData($userDataArray);
					}else{
						$userDataArray = array($userId, $trust, $respect, $care, $commitment, $financial_support, $domestric_support, $intimacy, $physical_appearance, $fun_time_together, $best_friend_talk, $spiritual_intimacy, $conflict_resolution, $isPresent,$cdate, $udate );				
						$this->UserDataModel->inserUserData($userDataArray);
					}
				}
				
				$userData = $this->UserDataModel->getUserData($userId, 2);
				
				$data['userData'] = $userData;
				$this->load->view('index_step3_2',$data);
			}else if($this->input->post('step') == "3_2"){
				$userData1 = $this->UserDataModel->getUserData($userId, 1);
				$userData2 = $this->UserDataModel->getUserData($userId, 0);
				
				$data['userData1'] = $userData1;
				$data['userData2'] = $userData2;
				$this->load->view('index_step6',$data);
			}elseif($this->input->post('step') == "6"){
				$this->load->library('email');
				$this->email->from('info@e-lizer.com', 'E-lizer');
				$this->email->to($adminInfor->email);
				//$this->email->cc('rri.tuan.dao@rri.vn');
				//$this->email->bcc('cometITvn@yahoo.com');
				
				$this->email->subject('E-lizer\'s notification');
				$this->email->message('<html>
											<body>
											<h4>Customer '.$user->first_name.' '.$user->last_name.' has just finished the test</h4>
											<p>Please go to <a href=\"'.base_url().'\">admin panel</a> to see more detail.</p>
											</body>
										</html>');
				
				$this->email->send();
				$this->load->view('index_step7',$data);
			}elseif($this->input->post('step') == "7"){
				redirect("logout");
			}
		}else{
			$this->load->view('index_welcome',$data);
		}
	}
}
